package com.algo.utils

import scala.collection.mutable.HashMap

object ContainerUtils {
  
  /**
   * combine map1 and map2 with function f
   * <br\>
   * changes happened to map2
   */
  def combine[K, V](map1: HashMap[K, V], map2:HashMap[K, V], f: ((V, V) => V))
  	:HashMap[K, V] = {
    map1.foreach(kv => {
      val v2 = map2.get(kv._1)
      val value = if(v2.isDefined)
        f.apply(kv._2, v2.get)
      else	kv._2
      
      map2.put(kv._1, value)
    })
    map2
  }
  /**
   * combine array1 and array2 with function f
   * <br\>
   * changes happened to array2
   */
  def combine[K](array1: Array[K], array2: Array[K], f: (K, K) => K)
  	:Array[K] = {
    for(i <- 0 to array1.length-1){
      array2(i) = f(array1(i), array2(i))
    }
    array2
  }
  
  def arrayToString[K](array: Array[K]): String = {
    val s = new StringBuilder
    array.foreach(elem => s.append(elem).append(","))
    s.deleteCharAt(s.size-1).toString
  }

}
